package com.roadjava.tutor.service.impl;

import com.roadjava.tutor.bean.dto.OrderInfoDTO;
import com.roadjava.tutor.bean.dto.TeacherJobDTO;
import com.roadjava.tutor.bean.dto.UserDTO;
import com.roadjava.tutor.bean.entity.Member;
import com.roadjava.tutor.bean.entity.TeacherJob;
import com.roadjava.tutor.bean.enums.OrderStatusEnum;
import com.roadjava.tutor.bean.res.ResultDTO;
import com.roadjava.tutor.bean.vo.TeacherJobVO;
import com.roadjava.tutor.bean.vo.UserVO;
import com.roadjava.tutor.mapper.MemberMapper;
import com.roadjava.tutor.mapper.OrderInfoMapper;
import com.roadjava.tutor.mapper.TeacherJobMapper;
import com.roadjava.tutor.service.MemberService;
import com.roadjava.tutor.service.TeacherJobService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhaodaowen
 * @see <a href="http://www.roadjava.com">乐之者java</a>
 */
@Service
public class TeacherJobServiceImpl implements TeacherJobService {
    @Resource
    private TeacherJobMapper teacherJobMapper;
    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Override
    public ResultDTO<List<TeacherJobVO>> loadTable(TeacherJobDTO dto) {
        List<TeacherJobVO> list = teacherJobMapper.listByPage(dto);
        if (CollectionUtils.isEmpty(list)) {
            return ResultDTO.buildSuccess(list, (long) list.size());
        }
        // 转成状态描述
        list.forEach(vo -> vo.setOrderStatus(OrderStatusEnum.getDescByStatus(vo.getOrderStatus())));
        long count = teacherJobMapper.selectCount(dto);
        return ResultDTO.buildSuccess(list,count);
    }

    @Override
    public ResultDTO<String> add(TeacherJobDTO dto) {
        TeacherJob teacherJob = dto.toEntity();
        teacherJobMapper.insert(teacherJob);
        return ResultDTO.buildSuccess("添加成功");
    }

    @Override
    public ResultDTO<String> deleteById(Long id) {
        // 被预约过的不能删除
        OrderInfoDTO orderInfoDTO = new OrderInfoDTO();
        orderInfoDTO.setJobId(id);
        int count = orderInfoMapper.selectCount(orderInfoDTO);
        if (count > 0) {
            return ResultDTO.buildFailure("该求职信息已被预约过,不能删除");
        }
        teacherJobMapper.deleteById(id);
        return ResultDTO.buildSuccess("删除成功");
    }
}
